#include "Gate.h"
#include "Events\Event.h"
#include "Camera.h"
#include "Time.h"

#include "Physics.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"

// Constructor
Gate::Gate(void)
{
	objectID = "Gate";
	StartOpen = false;

	retract = false;
	vertical = true;
	minHeight = 0;
}

// Destructor
Gate::~Gate(void)
{
}

void Gate::SetVertical(bool vertical)
{
	this->vertical = vertical;
}

void Gate::Update(float deltaTime)
{

	if(retract == true)
	{
		if(vertical)
		{
			hitBox.bottom -= (long)(100.0f * deltaTime);

			if(hitBox.bottom - hitBox.top < minHeight)
				hitBox.bottom = hitBox.top + minHeight;
		}
		else
		{
			hitBox.right -= (long)(100.0f * deltaTime);

			if(hitBox.right - hitBox.left < minHeight)
				hitBox.right = hitBox.left + minHeight;
		}
	}
	else
	{
		if(vertical)
		{
			hitBox.bottom += (long)(100.0f * deltaTime);

			if(hitBox.bottom - hitBox.top > maxHeight)
				hitBox.bottom = hitBox.top + maxHeight;
		}
		else
		{
			hitBox.right += (long)(100.0f * deltaTime);

			if(hitBox.right - hitBox.left > maxHeight)
				hitBox.right = hitBox.left + maxHeight;
		}
	}
}
void Gate::Render(void) const
{
	RECT box = hitBox;
	CSGD_TextureManager::GetInstance()->Draw(imageID,
		(int)(positionX - Camera::GetInstance()->OffsetX()),
		(int)(positionY - Camera::GetInstance()->OffsetY()), 1.0f, 1.0f, &box);

#if DEBUG_COLLISION
	RECT dbg_box = hitBox;
	dbg_box.left = (int)(dbg_box.left - Camera::GetInstance()->OffsetX());
	dbg_box.right = (int)(dbg_box.right - Camera::GetInstance()->OffsetX());
	dbg_box.bottom = (int)(dbg_box.bottom - Camera::GetInstance()->OffsetY());
	dbg_box.top = (int)(dbg_box.top - Camera::GetInstance()->OffsetY());

	CSGD_Direct3D::GetInstance()->DrawHollowRect(dbg_box,D3DCOLOR_ARGB(255,255,0,0));
#endif
}
void Gate::HandleCollision(GameObject& other, CollisionInfo& info)
{
	if(other.GetObjectID() == "Metal Box" || other.GetObjectID() == "Wood Box" || other.GetObjectID() == "Radiation Barrel")
	{
		//Handles the collision for anything that collides with a wall / Floor / Barrier of some sort.
		if(info.Side == CollisionSide::BOTTOM)
			other.SetPosY( other.GetPosY() - (info.Overlap.bottom - info.Overlap.top));
		if(info.Side == CollisionSide::TOP)
			other.SetPosY( other.GetPosY() + (info.Overlap.bottom - info.Overlap.top));
		if(info.Side == CollisionSide::LEFT)
			other.SetPosX( other.GetPosX() + (info.Overlap.right - info.Overlap.left));
		if(info.Side == CollisionSide::RIGHT)
			other.SetPosX( other.GetPosX() - (info.Overlap.right - info.Overlap.left));
		other.SetHitBox(Physics::UpdateColRect(other.GetHitBox(), (int)(other.GetPosX()), (int)(other.GetPosY())));
	}
}

void Gate::HandleEvent(Event* e)
{
	if(e->GetEventID() == "ButtonActivated")
	{
		vector<string>* tags = getSenderTags(e->GetEventID());

		for(unsigned int i = 0; i < tags->size(); ++i)
		{
			if(((GameObject*)e->GetSender())->GetTag() == (*tags)[i])
			{
				if(StartOpen == false)
					retract = true;
				else 
					retract = false;
				break;
			}
		}
	}
	else if(e->GetEventID() == "ButtonDeactivated")
	{
		vector<string>* tags = getSenderTags(e->GetEventID());

		for(unsigned int i = 0; i < tags->size(); ++i)
		{
			if(((GameObject*)e->GetSender())->GetTag() == (*tags)[i])
			{
				if(StartOpen == false)
					retract = false;
				else 
					retract = true;
				break;
			}
		}
	}
	else if(e->GetEventID() == "MachineFixed")
	{
		vector<string>* tags = getSenderTags(e->GetEventID());

		for(unsigned int i = 0; i < tags->size(); ++i)
		{
			if(((GameObject*)e->GetSender())->GetTag() == (*tags)[i])
			{
				if(StartOpen == false)
					retract = true;
				else 
					retract = false;
				break;
			}
		}
	}
	else if(e->GetEventID() == "Receiver Activate")
	{
		vector<string>* tags = getSenderTags(e->GetEventID());

		for(unsigned int i = 0; i < tags->size(); ++i)
		{
			if(((GameObject*)e->GetSender())->GetTag() == (*tags)[i])
			{
				if(StartOpen == false)
					retract = true;
				else 
					retract = false;
				break;
			}
		}
	}
	else if(e->GetEventID() == "Receiver Deactivate")
	{
		vector<string>* tags = getSenderTags(e->GetEventID());

		for(unsigned int i = 0; i < tags->size(); ++i)
		{
			if(((GameObject*)e->GetSender())->GetTag() == (*tags)[i])
			{
				if(StartOpen == false)
					retract = false;
				else 
					retract = true;
				break;
			}
		}
	}

}

void Gate::SetMinHeight(int minHeight)
{
	this->minHeight = minHeight;
}

void Gate::SetMaxHeight(int maxHeight)
{
	this->maxHeight = maxHeight;
}

void Gate::Retract(bool retract)
{
	this->retract = retract;
}

void Gate::SetStartOpen(bool Open)
{
	StartOpen = Open;
	retract = true;
}